package com.qian.ddj.settlement;

import com.qian.api.ddj.model.User;
import com.qian.api.ddj.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class Job {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAliCodeService aliCodeService;
    @Autowired
    private IAliCode2Service aliCode2Service;
    @Autowired
    private IBalanceSnapshotService balanceSnapshotService;

    /**
     * 结算
     */
    @Scheduled(cron = "0 0 */1 * * ?")
    public void settlement() {
        List<User> users = userService.findAll();
        for (User user : users) {
            log.info("settlement begin" + user.getUsername());
            try {
                orderService.settlement(user.getId());
            } catch (Exception e) {
                log.info(user.getUsername() + " 结算出错");
                log.info(e.getMessage());
            }
            log.info("settlement end" + user.getUsername());
        }
    }

    /**
     * 回收二维码
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void clearAliCode() {
        aliCodeService.clear();
    }

    /**
     * 回收二维码
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void clearAliCode2() {
        aliCode2Service.clear();
    }

    /**
     * 记录用户余额
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void balanceSnapshot() {
        balanceSnapshotService.balanceSnapshot();
    }
}
